import { SvgIcon } from '@jl/common';
import { svgIconPlugin } from '@jl/common/plugin';
import { useUserStore } from '@jl/common/store';
import { InvalidCodeEnum, setGlobalRequestConfig } from '@jl/common/utils';
import { messageUtil } from '@jl/element-plus/utils';
import { App } from 'vue';

export const installPlugin = (app: App) => {
  app.component('SvgIcon', SvgIcon);
  app.use(svgIconPlugin);

  setGlobalRequestConfig({
    timeout: 6000,
    messageUtil,
    responseInterceptor(data) {
      const userStore = useUserStore();
      const { code } = data;

      // 票据失效
      if (InvalidCodeEnum[+code]) {
        userStore.reset();
        window.location.reload();
        return false;
      }

      return true;
    },
  });
};
